package com.serkol.ams.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.primitives.Ints;
import com.serkol.ams.entity.LoginLog;
import com.serkol.ams.entity.Notify;
import com.serkol.ams.entity.NotifyUsers;
import com.serkol.ams.mapper.NotifyMapper;
import com.serkol.ams.service.NotifyService;
import com.serkol.ams.service.NotifyUsersService;
import com.serkol.ams.service.UsersWalletLogService;
import com.serkol.constant.AuthConstant;
import com.serkol.handler.MyLambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author pingan
 * @description 针对表【notify】的数据库操作Service实现
 * @createDate 2022-12-05 01:44:36
 */
@Service
public class NotifyServiceImpl extends ServiceImpl<NotifyMapper, Notify>
        implements NotifyService {
    @Autowired
    private NotifyUsersService notifyUsersService;

    @Autowired
    private UsersWalletLogService usersWalletLogService;

    @Override
    public boolean sendMessage(int type, String message) {
        Notify notify = new Notify();
        notify.setType(String.valueOf(type));
        notify.setContent(message);
        notify.setIsAll(1);
        boolean save = this.save(notify);
        return save;
    }

    @Override
    public boolean sendMessageAll(int type, String message) {
        Notify notify = new Notify();
        notify.setType(String.valueOf(type));
        notify.setIsAll(1);
        notify.setContent(message);
        boolean save = this.save(notify);
        return save;
    }

    @Override
    public boolean sendMessageType(int[] type, String message) {
        Notify notify = new Notify();
        String join = Ints.join(",", type);
        notify.setType(join);
        notify.setIsAll(1);
        notify.setContent(message);
        boolean save = this.save(notify);
        return save;
    }

    @Override
    public boolean sendMessageUsers(long[] userIds, String message) {
        Notify notify = new Notify();
        notify.setType("5");
        notify.setContent(message);
        boolean save = this.save(notify);
        if (save) {
            List<NotifyUsers> users = new ArrayList<>();
            for (int i = 0; i < userIds.length; i++) {
                long userId = userIds[i];
                NotifyUsers notifyUsers = new NotifyUsers();
                notifyUsers.setUserId(userId);
                notifyUsers.setNotifyId(notify.getId());
                notifyUsers.setType("5");
                users.add(notifyUsers);

            }

            notifyUsersService.saveBatch(users);
        }

        return save;
    }

    @Override
    public boolean sendMessageUsersType(int type, long[] userIds, String message) {
        Notify notify = new Notify();
        notify.setType(String.valueOf(type));
        notify.setContent(message);
        boolean save = this.save(notify);
        if (save) {
            List<NotifyUsers> users = new ArrayList<>();
            for (int i = 0; i < userIds.length; i++) {
                long userId = userIds[i];
                NotifyUsers notifyUsers = new NotifyUsers();
                notifyUsers.setUserId(userId);
                notifyUsers.setNotifyId(notify.getId());
                notifyUsers.setType(String.valueOf(type));
                users.add(notifyUsers);

            }

            notifyUsersService.saveBatch(users);
        }
        return save;
    }

    @Override
    public boolean sendMessageUser(long userId, String message) {
        Notify notify = new Notify();
        notify.setType("5");
        notify.setContent(message);
        boolean save = this.save(notify);
        if (save) {

            NotifyUsers notifyUsers = new NotifyUsers();
            notifyUsers.setUserId(userId);
            notifyUsers.setNotifyId(notify.getId());
            notifyUsers.setType("5");
            notifyUsersService.save(notifyUsers);
        }
        return save;

    }

    @Override
    public boolean sendMessageUserType(int type, long userId, String message) {
        Notify notify = new Notify();
        notify.setType(String.valueOf(type));
        notify.setContent(message);
        boolean save = this.save(notify);
        if (save) {

            NotifyUsers notifyUsers = new NotifyUsers();
            notifyUsers.setUserId(userId);
            notifyUsers.setNotifyId(notify.getId());
            notifyUsers.setType(String.valueOf(type));
            notifyUsersService.save(notifyUsers);
        }
        return save;
    }

    @Override
    public boolean sendMessageUserType(int[] type, long userId, String message) {
        Notify notify = new Notify();
        String join = Ints.join(",", type);
        notify.setType(join);
        notify.setContent(message);
        boolean save = this.save(notify);
        if (save) {
            NotifyUsers notifyUsers = new NotifyUsers();
            notifyUsers.setUserId(userId);
            notifyUsers.setNotifyId(notify.getId());
            notifyUsers.setType(join);
            notifyUsersService.save(notifyUsers);
        }
        return save;
    }

    @Override
    public Map<String, Object> mySystemNotify() {
        LoginLog loginLog = AuthConstant.getLoginLog();
        List<String> types = new ArrayList<>();
        types.add("1");
        types.add("2");
        types.add("3");
        types.add("4");
        Iterator<String> iterator = types.iterator();
        Map<String, Object> map = new HashMap<>();
        while (iterator.hasNext()) {
            String next = iterator.next();
            MyLambdaQueryWrapper<NotifyUsers> mySystemNotifyQueryWrapper = new MyLambdaQueryWrapper<>();
            mySystemNotifyQueryWrapper.eq(NotifyUsers::getUserId, loginLog.getUserId()).eq(NotifyUsers::getStatus, 0);
            mySystemNotifyQueryWrapper.findInSet(NotifyUsers::getType, next);
            long count = notifyUsersService.count(mySystemNotifyQueryWrapper);
            map.put(next, count);

        }

        return map;
    }

    @Override
    public List<Map<String, Object>> findByMyList(int type) {
        List<Map<String, Object>> result = new ArrayList<>();
        LoginLog loginLog = AuthConstant.getLoginLog();
        LambdaUpdateWrapper<NotifyUsers> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.apply(type > 0, "FIND_IN_SET(" + type + ",TYPE)").eq(NotifyUsers::getUserId, loginLog.getUserId());
        lambdaUpdateWrapper.set(NotifyUsers::getStatus, 1);
        notifyUsersService.update(lambdaUpdateWrapper);

        switch (type) {
            case 1:
                break;
            case 2:
                break;
            case 3:
                result = usersWalletLogService.findByNotifyList(loginLog.getUserId());
                break;
            case 4:
                break;
        }

        return result;
    }
}




